<template>
  <DataTable searchPlaceholder="请输入用户相关信息" :columns="columns" dialogTitle="用户信息表" :formFields="formFields"
    :tableData="userlData" apiUrl="user" :putUser="putUser" :addUser="addUser" :delUser="delUser" />
</template>

<script setup>
import DataTable from '../../components/DataTable.vue';
import { ref, onMounted } from "vue";
import axios from "axios";

// 定义表格列配置
const columns = [
  { prop: 'nickName', label: '昵称', width: 'show-overflow-tooltip' },
  { prop: 'userName', label: '用户名', width: 'show-overflow-tooltip' },
  { prop: 'telphone', label: '电话', width: 'show-overflow-tooltip' },
]

// 定义表单字段配置
const formFields = [
  { label: '昵称', model: 'nickName', placeholder: '请输入昵称' },
  { label: '用户名', model: 'userName', placeholder: '请输入用户名' },
  { label: '电话', model: 'telphone', placeholder: '请输入电话' },
  { label: '密码', model: 'userPasswd', placeholder: '请输入密码' }
];
// 定义用户数据
const userlData = ref([])

// 获取数据
async function getUserData() {
  try {
    const response = await axios.get('api/patients/getAllUser')
    console.log("userData:", response.data.data)
    userlData.value = response.data.data
  } catch (error) {
    console.log(error)
  }
}

// 新增数据
async function addUser(formValue) {
  console.log("new")
  console.log(formValue)

  try {
    await axios.post("api/patients/addUser", formValue);
    console.log("user新增", formValue)
  } catch (error) {
    console.error("新增用户时出错:", error);
  }
}

// 编辑数据
async function putUser(formValue) {
  try {
    await axios.put(`api/patients/putUser/${formValue.id}`, formValue);
    console.log("user编辑", formValue)
  } catch (error) {
    console.error("编辑用户时出错:", error);
  }
}

// 删除数据确
async function delUser(index, row) {
  try {
    await axios.delete(`api/patients/delUser/${row.id}`);
    console.log("管理员信息删除：", row.id)
  } catch (error) {
    console.error("删除用户时出错:", error);
  }
}
onMounted(() => {
  getUserData()
})
</script>
